package com.chenza.stock.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.chenza.stock.bean.StockDay;
import com.chenza.stock.service.GainService;

@Controller
public class GainController {

	@Autowired
	private GainService gainService;

	@RequestMapping("/gain/gain.action")
	public ModelAndView gain(StockDay stockDay) {
		Map<String, Object> map = gainService.gain(stockDay);
		ModelAndView mav = new ModelAndView((String) map.get("nextUrl"));
		mav.addAllObjects(map);
		return mav;
	}

	public static void main(String[] args) {
		try {
			StringBuilder id = new StringBuilder("http://hq.sinajs.cn/list=sh");
			String in = "601007";
			id.append(in);
			URL url = new URL(id.toString());
			URLConnection conn = url.openConnection();
			InputStream is = conn.getInputStream();
			Scanner sc = new Scanner(is, "GBK");
			String msg = null;
			while (sc.hasNextLine()) {
				msg = sc.nextLine();
			}
			System.out.println(msg);
			String[] str = msg.split("\"");
			for (int i = 0; i < str.length; i++) {
				msg = str[1];
			}
			String[] strs = msg.split(",");
			System.out.println(Arrays.toString(strs));
			System.out.println(strs.toString());
			sc.close();
			is.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
